package com.chatroomserver.controller;

import cn.hutool.json.JSONObject;
import com.chatroomserver.annotation.UrlLimit;
import com.chatroomserver.annotation.Userid;
import com.chatroomserver.dto.UserDto;
import com.chatroomserver.entity.User;
import com.chatroomserver.service.UserService;
import com.chatroomserver.utils.ResultUtil;
import com.chatroomserver.vo.user.RegisterVo;
import com.chatroomserver.vo.user.UpdateUserVo;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    UserService userService;

    @PostMapping("/register")
    public Object register(@RequestBody @Valid RegisterVo registerVo) {
        User result = userService.register(registerVo);
        return ResultUtil.Succeed(result);
    }

    @UrlLimit
    @GetMapping("/list")
    public Object listUser() {
        List<UserDto> result = userService.listUser();
        return ResultUtil.Succeed(result);
    }

    @UrlLimit
    @GetMapping("/list/map")
    public Object listMapUser() {
        Map<String, UserDto> result = userService.listMapUser();
        return ResultUtil.Succeed(result);
    }

    @UrlLimit
    @GetMapping("/online/web")
    public Object onlineWeb() {
        List<String> result = userService.onlineWeb();
        return ResultUtil.Succeed(result);
    }

    @UrlLimit
    @PostMapping("/update")
    public Object updateUser(@Userid String userid, @RequestBody @Valid UpdateUserVo updateUserVo) {
        boolean result = userService.updateUser(userid, updateUserVo);
        return ResultUtil.ResultByFlag(result);
    }

    @UrlLimit
    @DeleteMapping("/delete")
    public Object deleteUser(@RequestParam String userId) {
        boolean result = userService.deleteUser(userId);
        return ResultUtil.ResultByFlag(result);
    }

    @PostMapping("/upload-avatar")
    public JSONObject uploadAvatar(@RequestParam("file") MultipartFile file,
                                   @Userid String userId) {
        String avatarUrl = userService.uploadAvatar(file, userId);
        return ResultUtil.Succeed(avatarUrl);
    }
}
